<style scoped src="./index.scss" lang="scss" rel="stylesheet/scss"></style>
<template src="./index.html"></template>
<script>
    import GoodsEditBase from "components/GoodsEditBase/index";
    import GoodsEditDeliver from "components/GoodsEditDeliver/index";
    import GoodsEditSpecs from "components/GoodsEditSpecs/index";
    import GoodsEditGroup from "components/GoodsEditGroup/index";
    import GoodsEditAttr from "components/GoodsEditAttr/index";
    import GoodsEditParam from "components/GoodsEditParam/index";
    import GoodsEditServe from "components/GoodsEditServe/index";
    import GoodsEditOther from "components/GoodsEditOther/index";
    import GoodsEditHtml from "components/GoodsEditHtml/index";
    export default {
        mixins: [Vue.prototype.$vFN.Mixins.Base, Vue.prototype.$vFN.Mixins.Edit],
        components: {
            GoodsEditBase,
            GoodsEditDeliver,
            GoodsEditSpecs,
            GoodsEditGroup,
            GoodsEditAttr,
            GoodsEditParam,
            GoodsEditServe,
            GoodsEditOther,
            GoodsEditHtml,
        },
        data() {
            return {
appWindow: window,
                editData: {
                    // 基础
                    base: null,
                    // 配送
                    deliver: null,
                    // 规格
                    specs: null,
                    // 分组
                    group: null,
                    // 属性
                    attr: null,
                    // 参数
                    param: null,
                    // 服务
                    serve: null,
                    // 其他
                    other: null,
                    // 富文本
                    html: null,
                },
                form: {},
                // 统一规格值
                skuObj: {},
                // 页面接口地址
                apiUrl: {
                    create: "mall-site-portal/product/create",
                    update: "mall-site-portal/product/update",
                    detail: "mall-site-portal/product/getItem",
                },
                // 表格查询数据
                mixinTable_tableSearchData: {
                    // 父参数
                    // dictType: "",
                },
                // 表格数据
                tableData: [],
                loading: true,
                detailInfo: null,
                // 配送模板信息
                templateOptions: [],
                load: {
                    all: false,
                    // 基础
                    base: false,
                    // 配送
                    deliver: false,
                    // 规格
                    specs: false,
                    // 分组
                    group: false,
                    // 属性
                    attr: false,
                    // 参数
                    param: false,
                    // 服务
                    serve: false,
                    // 其他
                    other: false,
                    // 富文本
                    html: false,
                },
            };
        },

        methods: {
            changeBase(e) {
                this.setChangeData("base", e);
            },
            changeDeliver(e) {
                this.setChangeData("deliver", e);
            },
            changeSpecs(e) {

                this.setChangeData("specs", e);
            },
            changeGroup(e) {
                this.setChangeData("group", e);
            },
            changeAttr(e) {
                this.setChangeData("attr", e);
            },
            changeParam(e) {
                this.setChangeData("param", e);
            },
            changeServe(e) {
                this.setChangeData("serve", e);
            },
            changeOther(e) {
                this.setChangeData("other", e);
            },
            changeHtml(e) {
                this.setChangeData("html", e);
            },
            setChangeData(type_, e) {
                this.editData[type_] = {
                    ...e.data
                };
                this.load[type_] = e.load;
                let _is = 0;
                for (let i in this.load) {
                    if (!this.load[i] && i != "all") {
                        _is += 1;
                    }
                }
                if (_is <= 0) {
                    this.$vFN.Tools.setLoad(false);
                    this.load['all'] = true
                }
            },
            hideBox() {

                this.loading = true;
                this.mixinEdit_editVisible = false;
                setTimeout(() => {
                    this.detailInfo = null;
                    this.$refs["GoodsEditBase"].uninstall();
                    this.$refs["GoodsEditDeliver"].uninstall();
                    this.$refs["GoodsEditSpecs"].uninstall();
                    this.$refs["GoodsEditGroup"].uninstall();
                    this.$refs["GoodsEditAttr"].uninstall();
                    this.$refs["GoodsEditParam"].uninstall();
                    this.$refs["GoodsEditServe"].uninstall();
                    this.$refs["GoodsEditOther"].uninstall();
                    this.$refs["GoodsEditHtml"].uninstall();
                }, 500)

            },


            // 初始化
            async init() {

                this.$vFN.Tools.setLoad(true)
                if (this.mixinEdit_editType == "add") {
                    this.mixinEdit_editTitle = "商品创建";

                } else if (this.mixinEdit_editType == "update") {
                    this.mixinEdit_editTitle = "商品修改";
                    let _data = await this.$vFN.Http.post({
                        url: this.apiUrl.detail,
                        data: {
                            id: this.id
                        },
                    },window);

                    if (_data._ok) {
                        this.detailInfo = _data.data
                    }
                }

                this.mixinEdit_editVisible = true;
                this.loading = false;
            },
            // 获取配送模板信息
            async getTemplate() {
                this.templateOptions = await this.$vFN.Tools.getSelectListSource({
                    url: "mall-site-portal/product/listFreight",
                    format: false,
                },window);
            },

            async opened() {

                await this.getTemplate();

                this.$refs["GoodsEditBase"].install({
                    type: this.mixinEdit_editType,
                    datasource: this.detailInfo
                });
               

                this.$refs["GoodsEditDeliver"].install({
                    type: this.mixinEdit_editType,
                    datasource: this.detailInfo
                });
               
                this.$refs["GoodsEditSpecs"].install({
                    type: this.mixinEdit_editType,
                    datasource: this.detailInfo
                });
              
                this.$refs["GoodsEditGroup"].install({
                    type: this.mixinEdit_editType,
                    datasource: this.detailInfo
                });
                this.$refs["GoodsEditAttr"].install({
                    type: this.mixinEdit_editType,
                    datasource: this.detailInfo
                });
                this.$refs["GoodsEditParam"].install({
                    type: this.mixinEdit_editType,
                    datasource: this.detailInfo
                });
                this.$refs["GoodsEditServe"].install({
                    type: this.mixinEdit_editType,
                    datasource: this.detailInfo
                });
                this.$refs["GoodsEditOther"].install({
                    type: this.mixinEdit_editType,
                    datasource: this.detailInfo
                });
                this.$refs["GoodsEditHtml"].install({
                    type: this.mixinEdit_editType,
                    datasource: this.detailInfo
                });


            },

            // 商品分类数据源


            async submit() {
                let _baseIs = await this.$refs["GoodsEditBase"].validate()
                console.log("_baseIs", _baseIs);
                let _deliverIs = await this.$refs["GoodsEditDeliver"].validate()
                console.log("_deliverIs", _deliverIs);
                let _specsIs = await this.$refs["GoodsEditSpecs"].validate()
                console.log("_specsIs", _specsIs);
                let _groupIs = await this.$refs["GoodsEditGroup"].validate()
                console.log("_groupIs", _groupIs);
                let _attrIs = await this.$refs["GoodsEditAttr"].validate()
                console.log("_attrIs", _attrIs);
                let _paramIs = await this.$refs["GoodsEditParam"].validate()
                console.log("_paramIs", _paramIs);
                let _serveIs = await this.$refs["GoodsEditServe"].validate()
                console.log("_serveIs", _serveIs);
                let _otherIs = await this.$refs["GoodsEditOther"].validate()
                console.log("_otherIs", _otherIs);
                let _htmlIs = await this.$refs["GoodsEditHtml"].validate()
                console.log("_htmlIs", _htmlIs);
                if (!_baseIs || !_specsIs || !_deliverIs || !_groupIs || !_attrIs || !_paramIs || !_serveIs || !
                    _otherIs || !_htmlIs) {
                    return
                }


                let _baseData = this.$refs["GoodsEditBase"].getData()
                let _deliverData = this.$refs["GoodsEditDeliver"].getData()
                let _specsData = this.$refs["GoodsEditSpecs"].getData()
                let _groupData = this.$refs["GoodsEditGroup"].getData()
                let _attrData = this.$refs["GoodsEditAttr"].getData()
                let _paramData = this.$refs["GoodsEditParam"].getData()
                let _serveData = this.$refs["GoodsEditServe"].getData()
                let _otherData = this.$refs["GoodsEditOther"].getData()
                let _htmlData = this.$refs["GoodsEditHtml"].getData()
                let _form = {
                    ..._baseData,
                    ..._deliverData,
                    ..._specsData,
                    ..._groupData,
                    ..._attrData,
                    ..._paramData,
                    ..._serveData,
                    ..._otherData,
                    ..._htmlData,
                };




                if (this.mixinEdit_editType == "add") {
                    let _data = await this.$vFN.Http.post({
                        url: this.apiUrl.create,
                        data: _form,
                    },window);
                    if (_data._ok) {
                        this.hideBox();
                        this.onSubmitSuccess();
                        this.$vFN.Tools.messageSuccess("添加成功");
                    }
                } else if (this.mixinEdit_editType == "update") {
                    _form.id = this.id;
                    let _data = await this.$vFN.Http.post({
                        url: this.apiUrl.update,
                        data: _form,
                    },window);
                    if (_data._ok) {
                        this.hideBox();
                        this.onSubmitSuccess();
                        this.$vFN.Tools.messageSuccess("修改成功");
                    }
                }
            },

            // 提交成功
            onSubmitSuccess() {
                this.onSuccess();
            },
            // 成功回调
            onSuccess() {},
            // 取消回调
            onCancel() {},
            // 失败回调
            onError() {},

        },
        mounted() {
            this.init();
        },
        activated() {},
    };
</script>